#! /usr/bin/env python
# -*- coding: utf-8 -*-

import urllib2


class CustomedRedirectHandler(urllib2.HTTPRedirectHandler):
    
    def http_error_302(self, req, fp, code, msg, headers):
        ret = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
        
        ret.status = code
        return ret
    
    http_error_301 = http_error_302
    
def do_main(args):
    url = args[0]
    
    http_h = urllib2.HTTPHandler(debuglevel = 1)
    opener = urllib2.build_opener(http_h, CustomedRedirectHandler())
    
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)'),
                        ('Accept-Language', 'zh-cn;en-us;q=0.7,en;q=0.3'),
#                       ('Accept-Encoding', 'gzip, deflate'),
                        ('Connection', 'keep-alive')]
    
    ret = opener.open(url)


if __name__ == '__main__':
    import sys
    args = sys.argv[1:]
    do_main(args)
    
    
    